package com.kurumi.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author lyh
 *
 */
public class ObjectUtils {

	/**
	 * 获取字符串对象
	 * @param obj
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String getStringValue(Object obj){
		if(obj instanceof String){
			String value = (String)obj;
			if("-".equalsIgnoreCase(value)
					|| "--".equalsIgnoreCase(value)){
				value = null;
			}
			return StringUtil.empty(value);
		}else if(obj instanceof Date){
			return DateUtil.dateFormat((Date)obj);
		}else if(obj instanceof Integer){
			return ""+(Integer)obj;
		}else if(obj instanceof Long){
			return NumberUtils.getString(new BigDecimal(""+(Long)obj), 2);
		}else if(obj instanceof Float){
			if(Float.isNaN((Float)obj)){
				return null;
			}
			return NumberUtils.getString(new BigDecimal(""+(Float)obj), 2);
		}else if(obj instanceof Double){
			if(Double.isNaN((Double)obj)){
				return null;
			}
			return NumberUtils.getString(new BigDecimal(""+(Double)obj), 2);
		}else if(obj instanceof BigDecimal){
			return NumberUtils.getString((BigDecimal)obj, 2);
		}else if(obj instanceof List){
			return String.join(";", (List)obj);
		}else{
			return null;
		}
	}
	
	
	
	/**
	 * 获取日期对象（yyyy-MM-dd HH:mm:ss）
	 * @param obj
	 * @return
	 */
	public static Date getDateTimeValue(Object obj){
		if(obj == null){
			return null;
		}else if(obj instanceof Date){
			return (Date)obj;
		}else if(obj instanceof String){
			Date dateTime = null;
			String value = (String)obj;
			if(value.length() < 11){
				try {
					dateTime =  DateUtil.dateParse(DateUtil.DATE_FORMATE, (String)obj);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				try {
					dateTime =  DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE, (String)obj);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			return dateTime;
		}
		else{
			return null;
		}
		
	}
	
	/**
	 * 获取日期对象（yyyy-MM-dd HH:mm:ss）
	 * @param obj
	 * @return
	 */
	public static Date getGTCDeteTimeValue(Object obj){
		if(obj == null){
			return null;
		}else if(obj instanceof Date){
			// sol查询数据多8小时 减去8小时
			Date date = (Date)obj;
			long time = date.getTime()- 8 * 60 * 60 * 1000;
			
			return new Date(time);
		}else if(obj instanceof String){
			Date dateTime = null;
			try {
				
				  SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				  dateTime = sd.parse((String)obj);
				  long rightTime = (long) (dateTime.getTime() - 8 * 60 * 60 * 1000);  //把当前得到的时间用date.getTime()的方法写成时间戳的形式，再加上8小时对应的毫秒数
				  dateTime = new Date(rightTime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return dateTime;
		}
		else{
			return null;
		}
		
	}
	
	
	/**
	 * 获取日期对象（yyyy-MM-dd）
	 * @param obj
	 * @return
	 */
	public static Date getDateValue(Object obj){
		if(obj == null){
			return null;
		}else if(obj instanceof Date){
			return (Date)obj;
		}
		else if(obj instanceof String){
			Date date = null;
			String value = (String)obj;
			if(value.length() < 11){
				try {
					date =  DateUtil.dateParse(DateUtil.DATE_FORMATE, (String)obj);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				try {
					date =  DateUtil.dateParse(DateUtil.DATE_TIME_FORMATE, (String)obj);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			return date;
		}
		else{
			return null;
		}
		
	}
	
	/**
	 * 获取BigDecimal对象
	 * @param obj
	 * @return
	 */
	public static BigDecimal getBigDecimalValue(Object obj){
		if(obj == null){
			return null;
		}else if(obj instanceof BigDecimal){
			return (BigDecimal)obj;
		}else if(obj instanceof Integer){
			return new BigDecimal(""+(Integer)obj);
		}else if(obj instanceof Float){
			if(Float.isNaN((Float)obj)){
				return null;
			}
			return new BigDecimal(""+(Float)obj);
		}else if(obj instanceof Long){
			return new BigDecimal(""+(Long)obj);
		}else if(obj instanceof Double){
			if(Double.isNaN((Double)obj)){
				return null;
			}
			return new BigDecimal(""+(Double)obj);
		}else if(obj instanceof String){
			try{
				return new BigDecimal((String)obj);
			}catch (Exception e) {
				// TODO: handle exception
				return null;
			}
			
		}else{
			return null;
		}
	}
	
	/**
	 * 获取Integer对象
	 * @param obj
	 * @return
	 */
	public static Integer getIntegerValue(Object obj){
		if(obj == null){
			return null;
		}else if(obj instanceof Integer){
			return (Integer)obj;
		}else if(obj instanceof Long){
			return ((Long)obj).intValue();
		}else if(obj instanceof Float){
			if(Float.isNaN((Float)obj)){
				return null;
			}
			return ((Float)obj).intValue();
		}else if(obj instanceof Double){
			if(Double.isNaN((Double)obj)){
				return null;
			}
			return ((Double)obj).intValue();
		}else if(obj instanceof BigDecimal){
			return ((BigDecimal)obj).intValue();
		}else if(obj instanceof String){
			try{
				return new Integer((String)obj);
			}catch (Exception e) {
				// TODO: handle exception
				return null;
			}
			
		}else{
			return null;
		}
	}
	
	/**
	 * 获取Float对象
	 * @param obj
	 * @return
	 */
	public static Float getFloatValue(Object obj){
		if(obj == null){
			return null;
		}else if(obj instanceof Integer){
			return new Float((Integer)obj);
		}else if(obj instanceof Float){
			if(Float.isNaN((Float)obj)){
				return null;
			}
			return (Float)obj;
		}else if(obj instanceof Long){
			return ((Long)obj).floatValue();
		}else if(obj instanceof Double){
			if(Double.isNaN((Double)obj)){
				return null;
			}
			return ((Double)obj).floatValue();
		}else if(obj instanceof BigDecimal){
			return ((BigDecimal)obj).floatValue();
		}else if(obj instanceof String){
			try{
				return new Float((String)obj);
			}catch (Exception e) {
				// TODO: handle exception
				return null;
			}
			
		}else{
			return null;
		}
	}

	/**
	 * 获取Float对象
	 * @param obj
	 * @return
	 */
	public static Double getDoubleValue(Object obj){
		if(obj == null){
			return null;
		}else if(obj instanceof Integer){
			return new Double((Integer)obj);
		}else if(obj instanceof Float){
			if(Float.isNaN((Float)obj)){
				return null;
			}
			return ((Float)obj).doubleValue();
		}else if(obj instanceof Long){
			return ((Long)obj).doubleValue();
		}else if(obj instanceof Double){
			if(Double.isNaN((Double)obj)){
				return null;
			}
			return (Double)obj;
		}else if(obj instanceof BigDecimal){
			return ((BigDecimal)obj).doubleValue();
		}else if(obj instanceof String){
			try{
				return new Double((String)obj);
			}catch (Exception e) {
				// TODO: handle exception
				return null;
			}
			
		}else{
			return null;
		}
	}


}
